<script setup>
import { onMounted } from 'vue'
import * as echarts from 'echarts'
import { Loading } from '@element-plus/icons-vue'

const props = defineProps({
  w: Number,
  currentLi: String,
  openDialog: Function,
  updateLoading: Boolean,
  reasoningLoading: Boolean,
  sampleLoading: Boolean
})

const barChart = (ele) => {
  const chart = echarts.init(document.querySelector(ele))

  const xdata = ['边侧推理', '云侧优先', '边侧优先']
  const dataArr = [7.5, 3.5, 2.5] // 对应边侧推理7-8s，云侧优先3-4s，边侧优先2-3s的平均值

  const option = {
    grid: {
      left: '5%',
      right: '5%',
      bottom: '5%',
      top: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      axisLabel: {
        formatter: '{value}s',
        textStyle: {
          color: '#09c4ca',
          fontSize: '12px'
        }
      },
      min: 0,
      max: 8,
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(9, 196, 202, 0.1)',
          type: 'dashed'
        }
      },
      axisLine: {
        lineStyle: {
          color: 'rgba(9, 196, 202, 0.2)',
          width: 1
        }
      },
      axisTick: {
        show: false
      }
    },
    yAxis: {
      type: 'category',
      data: xdata,
      axisLabel: {
        show: true,
        textStyle: {
          color: '#09c4ca',
          fontSize: 0.5 * props.w
        }
      },
      axisLine: {
        lineStyle: {
          color: 'rgba(9, 196, 202, 0.2)',
          width: 1
        }
      },
      axisTick: {
        show: false
      }
    },
    series: [
      {
        name: '策略',
        type: 'bar',
        data: dataArr,
        itemStyle: {
          normal: {
            color: function (params) {
              const colorList = [
                {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 1,
                  y2: 0,
                  colorStops: [{
                    offset: 0,
                    color: '#f29701'
                  }, {
                    offset: 1,
                    color: '#e07b00'
                  }]
                },
                {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 1,
                  y2: 0,
                  colorStops: [{
                    offset: 0,
                    color: '#09c4ca'
                  }, {
                    offset: 1,
                    color: '#3eacb4'
                  }]
                },
                {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 1,
                  y2: 0,
                  colorStops: [{
                    offset: 0,
                    color: '#09c4ca'
                  }, {
                    offset: 1,
                    color: '#3eacb4'
                  }]
                }
              ]
              return colorList[params.dataIndex]
            },
            borderWidth: 1,
            borderColor: 'rgba(9, 196, 202, 0.2)',
            borderRadius: [0, 4, 4, 0],
            shadowColor: 'rgba(9, 196, 202, 0.3)',
            shadowBlur: 10
          }
        },
        barWidth: '40%',
        emphasis: {
          itemStyle: {
            shadowBlur: 20,
            shadowColor: 'rgba(9, 196, 202, 0.5)'
          }
        }
      }
    ]
  }

  chart.setOption(option)

  // 添加窗口大小变化时的自适应
  window.addEventListener('resize', () => {
    chart.resize()
  })
}

onMounted(() => {
  barChart('.barChart')
})
</script>

<template>
  <div class="analyse">
    <div class="analyse-title">
      <span>云边分布式推理加速</span>
    </div>
    <div class="top5-content">
      <ul>
        <li @click="openDialog('update')">
          <div :class="props.currentLi === 'update' ? 'cicle-selected' : 'cicle'"></div>
          <div :class="props.currentLi === 'update' ? 'li-content-selected-right' : 'li-content'">
            <span>云边协同策略</span>
            <span v-if="updateLoading" class="loading-icon">
              <el-icon><Loading /></el-icon>
            </span>
          </div>
        </li>
        <li @click="openDialog('reasoning')">
          <div :class="props.currentLi === 'reasoning' ? 'cicle-selected' : 'cicle'"></div>
          <div
            :class="props.currentLi === 'reasoning' ? 'li-content-selected-right' : 'li-content'"
          >
            <span>边侧设备推理</span>
            <span v-if="reasoningLoading" class="loading-icon">
              <el-icon><Loading /></el-icon>
            </span>
          </div>
        </li>
        <li @click="openDialog('sample')">
          <div :class="props.currentLi === 'sample' ? 'cicle-selected' : 'cicle'"></div>
          <div :class="props.currentLi === 'sample' ? 'li-content-selected-right' : 'li-content'">
            <span>云边协同推理</span>
            <span v-if="sampleLoading" class="loading-icon">
              <el-icon><Loading /></el-icon>
            </span>
          </div>
        </li>
      </ul>
    </div>
    <div class="chart-container">
      <div class="barChart" style="width: 100%; height: 80%"></div>
    </div>
    <div style="position: absolute; right: 20%; bottom: 0">
      <!-- <div class="inference-display-container inference-preview"></div> -->
    </div>
  </div>
</template>

<style lang="scss" scoped>
.analyse {
  width: 17.5%;
  height: 70%;
  float: left;
  display: flex; // 添加 flex 布局
  flex-direction: column; // 垂直排列

  .analyse-title {
    width: 100%;
    height: 10%;
    background: url(../images/title.png) no-repeat center;
    background-size: 100%;
    color: #fff;
    text-align: center;
    display: table;
    font-size: 0.9rem;

    span {
      display: table-cell;
      vertical-align: middle;
      font-size: 1rem;
      font-weight: 500;
      letter-spacing: var(--letter-spacing-base);
    }
  }

  .top5-content {
    width: 100%;
    height: 55%;

    ul {
      width: 100%;
      height: 100%;

      li {
        width: 100%;
        height: 25%;
        margin-top: 5%;
        position: relative;

        .li-content {
          width: 90%;
          height: 100%;
          margin-left: 10%;
          background: url(../images/border.png) no-repeat center;
          background-size: contain;
          font-size: 0.9rem;
          font-weight: 400;
          letter-spacing: var(--letter-spacing-base);
          padding-top: 6%;
          color: #fff;
          box-sizing: border-box;
          display: flex;
          justify-content: center;
          cursor: pointer;

          span {
            margin-right: 2%;
          }
        }

        .cicle {
          content: '';
          display: block;
          position: absolute;
          width: 1.7rem;
          height: 1.7rem;
          background: url(../images/green.png) no-repeat center;
          background-size: 100%;
          left: 0;
          bottom: 0;
        }

        .cicle-selected {
          content: '';
          display: block;
          position: absolute;
          width: 1.7rem;
          height: 1.7rem;
          background: url(../images/orange.png) no-repeat center;
          background-size: 100%;
          left: 0;
          bottom: 0;
        }

        .li-content-selected-right {
          width: 90%;
          height: 100%;
          margin-left: 10%;
          background: url(../images/border2.png) no-repeat center;
          background-size: contain;
          font-size: 0.9rem;
          font-weight: 400;
          letter-spacing: var(--letter-spacing-base);
          padding-top: 6%;
          color: #fff;
          box-sizing: border-box;
          display: flex;
          justify-content: center;
          cursor: pointer;

          span {
            margin-right: 2%;
          }
        }
      }
    }
  }

  .chart-container {
    width: 100%;
    height: 35%;
    margin-bottom: 8%;
    margin-top: -10%;
  }

  // 添加发光动画效果
  @keyframes glowPulse {
    0% {
      box-shadow:
        0 0 5px #f29701,
        0 0 10px #f29701,
        0 0 15px #f29701;
      opacity: 0.8;
    }
    50% {
      box-shadow:
        0 0 10px #f29701,
        0 0 20px #f29701,
        0 0 30px #f29701;
      opacity: 1;
    }
    100% {
      box-shadow:
        0 0 5px #f29701,
        0 0 10px #f29701,
        0 0 15px #f29701;
      opacity: 0.8;
    }
  }

  @keyframes spin {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }

  // 修改 Loading 图标的样式
  .loading-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: rgba(242, 151, 1, 0.2);
    animation: glowPulse 1.5s ease-in-out infinite;
    margin-left: 8px;

    :deep(.el-icon) {
      font-size: 16px;
      color: #f29701;
      animation: spin 1s linear infinite;
    }
  }

  .inference-display-container {
    width: 26.25%;
    height: 35%;
    margin-top: 10px;
    padding: 10px;
    position: relative;
    border-radius: 10px;
    background: linear-gradient(135deg, rgba(62, 172, 180, 0.05) 0%, rgba(9, 196, 202, 0.1) 100%);
    backdrop-filter: blur(5px);
    border: 1px solid rgba(62, 172, 180, 0.2);
    box-shadow:
      0 0 20px rgba(62, 172, 180, 0.1),
      inset 0 0 20px rgba(62, 172, 180, 0.05);

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: 1px;
      background: linear-gradient(90deg, transparent, rgba(62, 172, 180, 0.3), transparent);
    }
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      height: 1px;
      background: linear-gradient(90deg, transparent, rgba(62, 172, 180, 0.3), transparent);
    }

    .chart-title {
      height: 20%;
      font-size: 0.85rem;
      font-weight: 400;
      letter-spacing: var(--letter-spacing-base);
      text-align: center;
      color: #fff;
    }
  }

  .inference-preview-container {
    width: 100%;
    margin-top: 20px;
    padding: 15px;

    .image-preview-container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 10px;
      margin-top: 15px;
      padding: 10px;
    }

    .preview-item {
      aspect-ratio: 1;
      border-radius: 4px;
      overflow: hidden;
      cursor: pointer;
      border: 1px solid rgba(62, 172, 180, 0.3);
      transition: all 0.3s ease;

      &:hover {
        transform: scale(1.05);
        border-color: #f29701;
        box-shadow: 0 0 10px rgba(242, 151, 1, 0.3);
      }
    }

    .preview-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .inference-preview {
    width: 100%;
    height: 45%; // 调整高度以适应右下角空间
    margin-top: -5%; // 微调位置

    .image-preview-container {
      padding: 10px;
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 8px;
      height: calc(100% - 40px); // 减去标题高度
    }

    .preview-item {
      aspect-ratio: 1;
      border: 1px solid rgba(62, 172, 180, 0.3);
      border-radius: 4px;
      overflow: hidden;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover {
        transform: scale(1.05);
        border-color: #f29701;
        box-shadow: 0 0 10px rgba(242, 151, 1, 0.3);
      }
    }

    .preview-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
}
</style>
